package com.group.eams.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.AssetDetailQueryDto;
import com.group.eams.pojo.dto.MaintenApplyQueryDto;
import com.group.eams.pojo.entity.*;
import com.group.eams.pojo.entity.MaintenApply;
import com.group.eams.pojo.entity.MaintenApply;
import com.group.eams.pojo.entity.MaintenApply;

import com.group.eams.pojo.vo.FixedAssetListVo;
import com.group.eams.pojo.vo.MaintenApplyListVo;
import com.group.eams.service.MaintenApplyService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 维保申请(MaintenApply)表控制层
 *
 * @author lqk
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/maintenApply")
public class MaintenApplyController {
    /**
     * 服务对象
     */
    @Autowired
    private MaintenApplyService maintenApplyService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "维保申请列表")
    public Result list(MaintenApplyQueryDto maintenApplyQueryDto) {

        //1.准备分页条件
        Page<MaintenApply> page = new Page<>(maintenApplyQueryDto.getPage(), maintenApplyQueryDto.getLimit());

        //2.设置查询条件
        Page<MaintenApplyListVo> maintenApplyByPage = maintenApplyService.findMaintenApplyByPage(page, maintenApplyQueryDto);

        return Result.buildSuccess(maintenApplyByPage.getTotal(), maintenApplyByPage.getRecords());
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找维保申请")
    public Result find(@RequestParam Long id) {
        MaintenApply maintenApply = maintenApplyService.getById(id);
        return Result.buildSuccess(maintenApply);
    }

    @RequestMapping(value = "/findByMaintenApplyNum", method = RequestMethod.GET)
    @ApiOperation(value = "根据单号查找资产申请")
    public Result findByMaintenApplyNum(@RequestParam String maintenApplyNum) {
        List<MaintenApply> maintenApply = maintenApplyService.findByMaintenApplyNum(maintenApplyNum);
        return Result.buildSuccess(maintenApply);
    }


    @RequestMapping(value = "/findDetail", method = RequestMethod.GET)
    @ApiOperation(value = "查找资产申请详细")
    public Result findDetail( AssetDetailQueryDto assetDetailQueryDto) {
        //设置查询单号
        assetDetailQueryDto.setMaintenApplyNum(assetDetailQueryDto.getMaintenApplyNum());
        Page<MaintenApply> page = new Page<>(assetDetailQueryDto.getPage(), assetDetailQueryDto.getLimit());
        Page<FixedAssetListVo> maintenApplyDetailByPage =
                maintenApplyService.findDetailByPage(page, assetDetailQueryDto);
        return Result.buildSuccess(maintenApplyDetailByPage.getTotal(), maintenApplyDetailByPage.getRecords());
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加维保申请")
    public Result add(@RequestBody MaintenApply maintenApply) {
        boolean result = maintenApplyService.save(maintenApply);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改维保申请")
    public Result update(@RequestBody MaintenApply maintenApply) {
        boolean result = maintenApplyService.updateById(maintenApply);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation("删除维保申请")
    public Result delete(@RequestParam Long id) {
        boolean result = maintenApplyService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove", method = RequestMethod.POST)
    @ApiOperation("批量删除维保申请")
    public Result batchDelete(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = maintenApplyService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation("查询所有维保申请")
    public Result findAll() {
        List<MaintenApply> maintenApplyList = maintenApplyService.list();
        return Result.buildSuccess(maintenApplyList);
    }

    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    @ApiOperation("批量添加保修申请")
    public Result batchAdd(@RequestBody List<MaintenApply> maintenApplys) {
        boolean result = maintenApplyService.saveBatch(maintenApplys);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ApiOperation("批量修改保修申请")
    public Result batchUpdate(@RequestBody List<MaintenApply> maintenApplys) {
        boolean result = maintenApplyService.updateBatchById(maintenApplys);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

